
import axios from "axios"


// 1 导出一个方法
export const getCurrentCity = () => {
  const localCity = JSON.parse(localStorage.getItem('hkzf_city'))
  if (!localCity) {
    return new Promise((resolve, reject) => {
      try {
        // 如果没有,就使用首页获取的定位的代码来获取,然后返回该城市的数据
        var myCity = new window.BMapGL.LocalCity();
        myCity.get(async (res) => {
          // 获取数据
          const response = await axios.get(`http://localhost:8080/area/info?name=${ res.name }`)
          // 存储值
          localStorage.setItem('hkzf_city', JSON.stringify(response.data.body))
          resolve(response.data.body)
        });
      } catch (error) {
        reject(error)
      }
    })
  }else {
    return Promise.resolve(localCity)
  }
}